package com.etrans.baf.analyse.task.swap.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.etrans.baf.analyse.task.swap.service.PlatformService;
import com.etrans.baf.analyse.util.CommonUtil;
import com.etrans.baf.analyse.util.Constants;
import com.etrans.baf.analyse.util.DateUtils;

@Component("platformSwapAnalyse")
@Controller
@RequestMapping("/task/swap/platformswap/*")
public class PlatformSwapAnalyse {

	@Autowired
	private PlatformService platformService;
	
	static final Logger logger = LoggerFactory.getLogger(PlatformSwapAnalyse.class);

	@RequestMapping(value = "reTimer.html")
	public void reTimer(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {	
			logger.info("平台重分析开始----------------分析日期:"+request.getParameter("date"));
			String dateStr = (request.getParameter("date")!=null&& !"".equals(String.valueOf(request.getParameter("date"))))?String.valueOf(request.getParameter("date")):null;
			String analyse_date = DateUtils.getAnalyseDate(dateStr,0);
			platformService.platformSwap(analyse_date,0);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);

	}
	@RequestMapping(value = "reTimer1.html")
	public void reTimer1(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("平台链路重分析----------------分析日期:"+request.getParameter("date"));
			String dateStr = (request.getParameter("date")!=null&& !"".equals(String.valueOf(request.getParameter("date"))))?String.valueOf(request.getParameter("date")):null;
			String analyse_date = DateUtils.getAnalyseDate(dateStr,0);
			platformService.platformSwap(analyse_date,1);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
	@RequestMapping(value = "reTimer2.html")
	public void reTimer2(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("平台得分重开始分析----------------分析日期:"+request.getParameter("date"));
			String dateStr = (request.getParameter("date")!=null && !"".equals(String.valueOf(request.getParameter("date"))))?String.valueOf(request.getParameter("date")):null;
			String analyse_date = DateUtils.getAnalyseDate(dateStr,0);
			platformService.updateScore(analyse_date);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
	@RequestMapping(value = "rePlatformWeek.html")
	public void rePlatformWeek(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("平台周分析重开始分析-------某个星期一的日期:date:2017-09-25 ;分析日期date="+request.getParameter("date"));
			String start_date = (request.getParameter("date")!=null)?String.valueOf(request.getParameter("date")):DateUtils.getLastWeekOne(null);
 			String end_date = DateUtils.getLastWeekSeven(start_date);
 			platformService.platformWMSY(start_date, end_date, Constants.PLATFORM_ANALYSE_W);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
	/*@RequestMapping(value = "rePlatformWeek_score.html")
	public void rePlatformWeek_score(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("平台周分析得分重开始分析-------某个星期一的日期:date:2017-09-25 ;分析日期date="+request.getParameter("date"));
			String start_date = (request.getParameter("date")!=null)?String.valueOf(request.getParameter("date")):DateUtils.getLastWeekOne(null);
			platformService.updateScoreWMSY(start_date, Constants.PLATFORM_ANALYSE_W);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}*/
	
	@RequestMapping(value = "rePlatformMonth.html")
	public void rePlatformMonth(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("平台月分析重开始分析------last_mon:如2017-08,current_mon:如2017-09： last_mon="+request.getParameter("last_mon")+";current_mon="+request.getParameter("current_mon"));
			String last_mon =  (request.getParameter("last_mon")!=null && !"".equals(String.valueOf(request.getParameter("last_mon"))))?String.valueOf(request.getParameter("last_mon")):DateUtils.getAnalyseDate(null, 1).substring(0,7);
			String current_mon = (request.getParameter("current_mon")!=null&& !"".equals(String.valueOf(request.getParameter("current_mon"))))?String.valueOf(request.getParameter("current_mon")):DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
			platformService.platformWMSY(last_mon+"-25",current_mon+"-24", Constants.PLATFORM_ANALYSE_M);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
	/*@RequestMapping(value = "rePlatformMonth_score.html")
	public void rePlatformMonth_score(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("平台月分析得分重分析-------分析月份：date_month格式:2017-09;参数:"+request.getParameter("date_month"));
			String current_mon = (request.getParameter("date_month")!=null && !"".equals(String.valueOf(request.getParameter("date_month"))))?String.valueOf(request.getParameter("date_month")):DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
			platformService.updateScoreWMSY(current_mon+"-01", Constants.PLATFORM_ANALYSE_M);
  			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}*/
	
	public void timer(){
		logger.info("开始分析----------------");
		analyse(null);
		logger.info("分析结束");
		 
	}
	public void timer1(){
		logger.info("开始分析----------------");
		try {
			String analyse_date = DateUtils.getAnalyseDate(null,0);//获取分析日期 
			platformService.platformSwap(analyse_date,1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("分析结束");
		 
	}
	public void timer2(){
		logger.info("开始分析----------------");
		try {
			String analyse_date = DateUtils.getAnalyseDate(null,0);//获取分析日期 
			platformService.updateScore(analyse_date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("分析结束");
		 
	}
	private void analyse(String date){
		try {
			String analyse_date = DateUtils.getAnalyseDate(date,0);
			platformService.platformSwap(analyse_date,0);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	 
	public void platformWeek(){
		try {
			String start_date = DateUtils.getLastWeekOne(null);
			String end_date = DateUtils.getLastWeekSeven(start_date);
			//platformService.platformWMSY("2017-09-19", "2017-09-21", Constants.PLATFORM_ANALYSE_W);
			platformService.platformWMSY(start_date, end_date, Constants.PLATFORM_ANALYSE_W);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void platformWeek_score(){
		try {
			String start_date = DateUtils.getLastWeekOne(null);
			String end_date = DateUtils.getLastWeekSeven(start_date);
			//platformService.updateScoreWMSY("2017-09-19", Constants.PLATFORM_ANALYSE_W);
			platformService.updateScoreWMSY(start_date, Constants.PLATFORM_ANALYSE_W);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//上月
	public void platformMonth(){
		try {
			String last_mon = DateUtils.getAnalyseDate(null, 1).substring(0,7);
			String current_mon = DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
			platformService.platformWMSY(last_mon+"-25",current_mon+"-24", Constants.PLATFORM_ANALYSE_M);
 		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void platformMonth_score(){
		try {
			String current_mon = DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
 			platformService.updateScoreWMSY(current_mon+"-01", Constants.PLATFORM_ANALYSE_M);
 		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//当月
	@RequestMapping(value = "platformCurMonth.html")
	public void platformCurMonth(){
		try {
			Date cdate = new Date();
			String current_mon_day = DateUtils.format(cdate,Constants.DATE_YYYY_MM_DD);
			String day = current_mon_day.substring(current_mon_day.length()-2,current_mon_day.length());
			String current_mon = DateUtils.format(cdate,Constants.DATE_YYYY_MM);
			if(Integer.parseInt(day)<=25){
				String last_mon = DateUtils.getAnalyseDate(null, 1).substring(0,7);
				platformService.platformWMSY(last_mon+"-25",current_mon+"-24", Constants.PLATFORM_ANALYSE_CM);
			}else{
  				String next_month = DateUtils.nextMonthFirstDate().substring(0,7);
				platformService.platformWMSY(current_mon+"-25",next_month+"-24", Constants.PLATFORM_ANALYSE_CM);
			}
 		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*public void platformCurMonth_score(){
		try {
			String current_mon_day = DateUtils.format(new Date(),Constants.DATE_YYYY_MM_DD);
			String day = current_mon_day.substring(current_mon_day.length()-2,current_mon_day.length());
			if(Integer.parseInt(day)<=25){
				String current_mon = current_mon_day.substring(0,7);
	 			platformService.updateScoreWMSY(current_mon+"-01", Constants.PLATFORM_ANALYSE_CM);
			}else{
	 			platformService.updateScoreWMSY(DateUtils.nextMonthFirstDate(), Constants.PLATFORM_ANALYSE_CM);
			}
 		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	
}
